	
	package Dao.daoimpl;
	
	import java.sql.Connection;
	import java.sql.PreparedStatement;
	import java.sql.ResultSet;
	import java.sql.SQLException;
	import java.sql.Statement;
	import java.util.ArrayList;
	import java.util.List;

	import unitls.DataBaseConnection;
	import Dao.UserDao;
	import enitys.User;
	
	public class UserDaoImpl implements UserDao {
		private  Connection conn = null;
		private Statement sta = null;
		private  ResultSet rs = null;
		String sql = null;
		private PreparedStatement pstmt = null;
		/**
		 * 验证登录(non-Javadoc)
		 * @see Dao.UserDao#CheckLogin(enitys.User)
		 */
		public boolean CheckLogin(String name,String password,int shenfen) throws Exception {
		//	System.out.print(name+password+shenfen);
			try{
				if(shenfen==1){
				sql="select * from admin where name='"+name+"' and pass='"+password+"'";
				}
				if(shenfen==0){
				sql="select * from user where username='"+name+"' and userpass='"+password+"'";
				}
				conn = DataBaseConnection.getConnection();
				sta = conn.createStatement();
				rs = sta.executeQuery(sql);
				 
				if(rs.next()){
					System.out.print(rs.getString(1)+" "+rs.getString(2));
					return true;
				}
			}catch(Exception e){
				e.printStackTrace();
				return false;	
			}finally{
				DataBaseConnection.closeRs(rs);
				DataBaseConnection.closeStmt(sta);
				DataBaseConnection.closeConn(conn);
			}
			return false;
			
		}
		
		
		
		
		/**
		 * 用户注册(non-Javadoc)
		 * @see Dao.UserDao#userRegister(enitys.User)
		 */
		public boolean userRegister(User user) throws Exception {
			// TODO Auto-generated method stub
			  sql = "select * from user where username=?"; 
				try  {
					 
					conn = DataBaseConnection.getConnection();
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, user.getUsername());
					rs = pstmt.executeQuery();
					if (!rs.next()) {
						sql="insert into user (username,userpass,uemail,class) values ('"+ user.getUsername()+"','"+
						user.getPassword()+"','"+user.getEmail()+"','"+user.getClasses()+"')";
						pstmt.executeUpdate(sql);
						return true;
					    }
					 
				}
				catch (Exception e) {
				   e.printStackTrace();  
				} finally {
					DataBaseConnection.closeRs(rs);
					DataBaseConnection.closeStmt(sta);
					DataBaseConnection.closeConn(conn);
				}
				 return false;
		}
		
		
		
		/**
		 * 查询学生信息(non-Javadoc)
		 * @see Dao.UserDao#findUser(enitys.User)
		 */
		public List<User> findUser() throws Exception {
			// TODO Auto-generated method stub
	
			String sql = "select * from user"; // 用于存储查询的结果
			List<User> list = new ArrayList<User>();// 用List集合来保存学生的信息
			this.conn = DataBaseConnection.getConnection();
			try {
				this.sta = this.conn.createStatement();
				this.rs = this.sta.executeQuery(sql);
			while(rs.next()) {
				User user=new User();
				user.setS_id(rs.getInt(1));
				user.setUsername(rs.getString(2));
				user.setPassword(rs.getString(3));
				user.setEmail(rs.getString(4));
				user.setClasses(rs.getString(5));
				list.add(user);//user的结果依次添加在list集合中
			}
			}catch(Exception e){
				e.printStackTrace();
				// 若查询过程中出现异常 则返回null
				return null;
			}finally {
				// 关闭连接
				DataBaseConnection.closeRs(this.rs);
				DataBaseConnection.closeStmt(this.sta);
				DataBaseConnection.closeConn(this.conn);
			}
			return list;
		}
		
		/**
		 * 按Id查看信息
		 * 
		 */

		public User findUserByName(String name) throws Exception {
			// TODO Auto-generated method stub
			this.sql = "select * from user where username = '" + name + "'";
			this.conn = DataBaseConnection.getConnection();
			// 用于存放数据
			User result = new User();
			try {
				this.sta = this.conn.createStatement();
				this.rs = this.sta.executeQuery(this.sql);
				if (rs.next()) {
					result.setS_id(this.rs.getInt("s_id"));
					result.setUsername(this.rs.getString("username"));
					result.setEmail(this.rs.getString("uemail"));
					result.setClasses(this.rs.getString("class"));
					result.setPassword(this.rs.getString("userpass"));
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				// 关闭连接
				DataBaseConnection.closeRs(this.rs);
				DataBaseConnection.closeStmt(this.sta);
				DataBaseConnection.closeConn(this.conn);
			}
			return result;
		}
		
		/**
		 * 按学号来查询信息(non-Javadoc)
		 * 
		 * @see Dao.UserDao#getuser(int)
		 */
		public User getuserById(int s_id) throws Exception {
			// TODO Auto-generated method stub
			this.sql = "select * from user where s_id = '" + s_id + "'";
			//取得数据库的链接
			this.conn = DataBaseConnection.getConnection();
			// 用于存放数据
			User result = new User();
			try {
				this.sta = this.conn.createStatement();
				this.rs = this.sta.executeQuery(this.sql);
				while(this.rs.next()) {
					result.setS_id(this.rs.getInt("s_id"));
					result.setUsername(this.rs.getString("username"));
					result.setEmail(this.rs.getString("uemail"));
					result.setClasses(this.rs.getString("class"));
					result.setPassword(this.rs.getString("userpass"));
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				// 关闭连接
				DataBaseConnection.closeRs(this.rs);
				DataBaseConnection.closeStmt(this.sta);
				DataBaseConnection.closeConn(this.conn);
			}
			return result;
		}
	
		
		
		/**
		 * 修改学生信息(non-Javadoc)
		 * @see Dao.UserDao#modfiyUser(enitys.User)
		 */
		public boolean modfiyUser(User user,int id) throws Exception {
			// TODO Auto-generated method stub

			String name = user.getUsername();
			String password = user.getPassword();
			String email = user.getEmail();
			String Class=user.getClasses();
			this.sql = "update user set username = '" + name + "',userpass = '" + password + "' ,class = '" + Class
					+ "' , uemail = '" + email + "' where s_id = '" + id + "'";
			this.conn = DataBaseConnection.getConnection();
			try {
				this.sta = this.conn.createStatement();
				this.sta.executeUpdate(this.sql);
				return true;
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				// 关闭连接
				DataBaseConnection.closeStmt(this.sta);
				DataBaseConnection.closeConn(this.conn);
			}

			return false;

		}
		
		
		
		
		
		/**
		 * 删除学生信息(non-Javadoc)
		 * @see Dao.UserDao#deleteUser(enitys.User)
		 */
			public int deleteUser(int  id) throws Exception {
			// TODO Auto-generated method stub
				int result=0;
				try {
					sql="delete from user where s_id = "+id;
				
					conn=DataBaseConnection.getConnection();
					sta=DataBaseConnection.getStatement(conn);
					result=sta.executeUpdate(sql);
				}  catch (Exception e) {
					e.printStackTrace();
				} finally {
					try {
						if(sta != null) {
							sta.close();
							sta= null;
						}
						if(conn != null) {
							conn.close();
							conn = null;
						}
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				return result;
		}







	
	}
